package com.wky.user.controller;

import com.wky.basic.Result.JsonResult;
import com.wky.basic.exception.BusinessException;
import com.wky.user.service.VerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
@CrossOrigin
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 获取验证码图片base64编码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public JsonResult getVerifyCodeImage(@PathVariable String key) {
        try {
            String base64Img = verifyCodeService.verifyCodeImage(key);
            return JsonResult.success(base64Img);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 手机号发送验证码
     */
    @PostMapping("/send")
    public JsonResult sendVerifyCode(@RequestBody Map<String,String> map) {
        try {
            String send = verifyCodeService.sendVerifyCode(map);
            return JsonResult.success(send);
        }  catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 邮箱发送验证码
     */
    @PostMapping("/sendEmailCode")
    public JsonResult emailVerifyCode(@RequestBody Map<String, String> map) {
        try {
            String send = verifyCodeService.emailVerifyCode(map);
            return JsonResult.success(send);
        }  catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

}
